import time
import flask
import pymongo
import json
from spider_xue.settings import NINE_TIME_STAMP
from collections import OrderedDict
from db import *

def search_content_1(key):
    myclient = pymongo.MongoClient("mongodb://115.159.72.159:7891/")
    db = myclient.admin
    # 认证
    db.authenticate('adminkpmg', 'kpmg123')
    mydb = myclient["spider"]
    mycol = mydb["msg"]
    print(key, 111111111)
    gg = mycol.find({"content": {"$regex": key}})
    r = []
    for i in gg:
        r.append({"time": i['time'], "content": i['content']})
    r.reverse()
    return r

def search_content_22222(key):
    
    gg = record_db.find({"ts": {"$gte": NINE_TIME_STAMP}}).sort([('ts', -1)])
    r = []
    h = OrderedDict()
    for i in gg:
        if i['symbol'] not in h:
            h[i['symbol']] = []
        h[i['symbol']].append(time.strftime('%H:%M', time.localtime(i['ts'])))
    
    for k, v in h.items():
        stocks = stock_db.find_one({"symbol": k})
        # hxtc = stocks['ths_hxtc']
        url1 = f"https://wap.eastmoney.com/quote/stock/{k.replace('SH', '1.').replace('SZ', '0.')}.html"
        r.append({"key": k+stocks['name'], "time": ', '.join(v[:10]), 'hxtc': stocks['ths_hxtc'], 'high': str(stocks.get('high', 0)) + ', ' + str(stocks.get('macd', 0)), 'url': url1})
    return r